(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.feeCode.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'feeCodeService' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, feeCodeService) {
		var ctrl = this, feeCodeInfo = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("feeCodeCreate")
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.feeCode = {};
		
		ctrl.init = function() {
			// init tier criteria
			feeCodeService.initTierCriteria(layoutConfig, $scope, ctrl);
			feeCodeService.initOptionData(ctrl, $scope).then(function() {
				authService.create().then(function(response) {
					var initFeeCode = response.data || {};
					initFeeCode.action = 'create';
					feeCodeService.initLayoutAndModel($scope, ctrl, initFeeCode, layoutConfig, false).then(function() {
					});
				});
			});
		};

		ctrl.saveFeeCode = function() {
			var messages = [];
			//
			var msg = feeCodeService.validateTierCriteria($scope, ctrl);
			if (msg) messages.push(msg);
			
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.feeCode.taskId) {
				return authService.createContinueSave(ctrl.feeCode).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.feeCode).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.submitFeeCode = function() {
			var messages = [];
			//
			var msg = feeCodeService.validateTierCriteria($scope, ctrl);
			if (msg) messages.push(msg);
			
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.feeCode.taskId) {
				return authService.createContinueSubmit(ctrl.feeCode).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.feeCode).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.feeCode.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.feecode.list',
					'state' : 'cda.feeCode.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
